// pages/operation/cARecord/cARecordIndex/index.js
import {
  CARecord
} from "../../../../api/api-work/cARecord.js"
const cARecord = new CARecord()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    param: "",
    amount: "", //课消金额
    pageNum: 1,
    pageSize: 15,
    total: 0,
    tableDatas: [],
    istriggered: false,
    loadStatus: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    this.setData({
      pageNum: 1,
      tableDatas: []
    })
    await this.getOrderListThird()
    await this.getOrderCountSecond()
  },
  //获取课次学员列表
  async getOrderListThird() {
    cARecord.getOrderListThirdApi({
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      name: this.data.param
    }).then(res => {
      let rows = this.data.tableDatas;

      rows.push(...res.rows);
      // rows.forEach((item) => {
      //   if (item.classDate) {
      //     const dateTimeString = item.classDate;
      //     const date = new Date(dateTimeString.replace(" ", "T")); 
      //     const year = date.getFullYear();
      //     const month = String(date.getMonth() + 1).padStart(2, "0"); 
      //     const day = String(date.getDate()).padStart(2, "0");
      //     item.classDate = `${year}-${month}-${day}`;
      //   }
      // });
      this.setData({
        tableDatas: rows,
        total: res.total
      })
      if (this.data.total == 0) {
        this.setData({
          // 无数据
          loadStatus: ""
        })
      } else if (this.data.tableDatas.length < this.data.total) {
        // 还有数据
        this.setData({
          // 加载更多
          loadStatus: "-"
        })
      } else {
        // 没有数据了
        this.setData({
          // 加载更多
          loadStatus: "nomore"
        })
      }
    })
  },
  searchStudent() {
    this.setData({
      pageNum: 1,
      tableDatas: []
    })
    this.getOrderListThird();
  },
  // 上拉加载
  getMoreDatas() {
    if (this.data.loadStatus === 'nomore') {
      // 没有更多了
      return;
    }
    this.setData({
      pageNum: this.data.pageNum + 1,
      loadStatus: "loading" // 加载中
    })
    this.getOrderListThird();
  },

  // 下拉刷新
  refreshMoreDatas() {
    this.setData({
      istriggered: false,
      pageNum: 1,
      tableDatas: []
    })
    this.getOrderListThird();
  },
  //获取课消金额
  async getOrderCountSecond() {
    cARecord.getOrderCountSecondApi({}).then(res => {
      console.log(res);
      this.setData({
        amount: res.amount
      })
    })
  },
  //去课消记录详情
  tocARecordDetails(e) {
    console.log('eeee', e);
    // const param = JSON.stringify(e.currentTarget.dataset.query)
    wx.navigateTo({
      url: `/pages/operation/cARecord/cARecordDetial/index?id=${e.currentTarget.dataset.query.id}`,
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})